package com.buildings.http;


import android.text.TextUtils;
import android.util.Log;

import com.buildings.base.BaseApplication;
import com.buildings.util.SPUtil;

import org.json.JSONException;

import java.util.Map;

import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.Request;
import okhttp3.RequestBody;

/**
 * Created by songwei on 2017/3/6.
 */

public class MyHttpClient {

    private Builder mBuilder;
//    private JSONObject json = new JSONObject();

    private MyHttpClient(Builder builder){
        this.mBuilder = builder;
    }

    public Request buildRequest(){

        Request.Builder builder = new Request.Builder();


        if (mBuilder.method == "GET"){
            builder.get();
            builder.url(mBuilder.url);

        }else if (mBuilder.method == "POST"){

            try {
                builder.post(buildRequestBody());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            builder.url(mBuilder.url);
        }else if(mBuilder.method == "PATCH"){
            try {
                builder.patch(buildRequestBody());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            builder.url(mBuilder.url);
        }
        String token = (String) SPUtil.get("token","");
        if(!TextUtils.isEmpty(token)){
            builder.addHeader("token",token);
            Log.e("token",token+"");
        }
        return  builder.build();
    }

    private RequestBody buildRequestBody() throws JSONException {

        String data = "";

        if (mBuilder.isJsonParam){


                data = mBuilder.mJsonParams;

                Log.e("APP上传的数据= ", data + "");

            if (!data.equals("")){
                return RequestBody.create(MediaType.parse("application/json; charset=utf-8"),data);
            }else {
                if(mBuilder.params != null){
                    MultipartBody.Builder builder = new MultipartBody.Builder();
                    builder.setType(MultipartBody.FORM);
                    for(Map.Entry<String,String> entry : mBuilder.params.entrySet()){
                        String key = entry.getKey();
                        String value = entry.getValue();
                        builder.addFormDataPart(key,value);
                    }
                    RequestBody requestBody = builder.build();
                    return requestBody;
                }
            }
        }


        FormBody.Builder builder = new FormBody.Builder();
        return builder.build();
    }


    public void enqueue(BaseCallBack callBack){

        OkHttpManager.getHttpManager().request(this,callBack);
    }

    public  static Builder newBuilder(){

        return new Builder();
    }

    public static class Builder {

        private String url;
        private String method;
        private boolean isJsonParam;


        private String mJsonParams = "";
        private Map<String,String> params;


        private Builder(){

            method = "POST";
        }

        public MyHttpClient build(){

            return new MyHttpClient(this);
        }


        public Builder url(String url){
            Log.e("请求的Url是 " , url);
            this.url = url;
            return this;
        }


        public Builder post(){
            method = "POST";
            isJsonParam = true;
            return this;
        }

        public Builder patch(){
            method = "PATCH";
            isJsonParam = true;
            return this;
        }

        public Builder get(){
            method = "GET";
            isJsonParam = false;
            return this;
        }

        public Builder addParam(String json){

            this.mJsonParams = json;

            return this;
        }

        public Builder addParam(Map<String,String> params){
            this.params = params;
            return this;
        }
    }
}
